package com.dckeji.shiplock.framework.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/12
 * Time: 14:37
 */
public class WeChatUtil {


    public static String reject(String openId, String templateId, String content, String opt, String rejectDesc) {
        String wxText = "{\n" +
                "    \"touser\": \"" + openId + "\",\n" +
                "    \"template_id\": \"" + templateId + "\",\n" +
                "    \"topcolor\": \"#FF0000\",\n" +
                "    \"data\": {\n" +
                "        \"keyword1\": {\n" +
                "            \"value\": \"" + content + opt + "\",\n" +
                "            \"color\": \"#173177\"\n" +
                "        },\n" +
                "        \"keyword2\": {\n" +
                "            \"value\": \"拒绝 \",\n" +
                "            \"color\": \"#173177\"\n" +
                "        },\n" +
                "        \"remark\": {\n" +
                "            \"value\": \"拒绝原因:" + rejectDesc + "\",\n" +
                "            \"color\": \"#EE113D\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        return wxText;
    }

    public static String success(String openId, String templateId, String content, String opt, String rejectDesc) {
        String wxText = "{\n" +
                "    \"touser\": \"" + openId + "\",\n" +
                "    \"template_id\": \"" + templateId + "\",\n" +
                "    \"topcolor\": \"#FF0000\",\n" +
                "    \"data\": {\n" +
                "        \"keyword1\": {\n" +
                "            \"value\": \"" + content + opt + "\",\n" +
                "            \"color\": \"#173177\"\n" +
                "        },\n" +
                "        \"keyword2\": {\n" +
                "            \"value\": \"通过\",\n" +
                "            \"color\": \"#173177\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        return wxText;
    }

    public static String sin(String ticket, String timestamp, String noncestr, String Referer) {
        MessageDigest crypt = null;
        String signature = "";
        String str = "jsapi_ticket=" + ticket +
                "&noncestr=" + noncestr +
                "&timestamp=" + timestamp +
                "&url=" + Referer;
        System.out.println(str);
        try {
            crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(str.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return signature;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}
